﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace MakeItWeb.Framework
{
    [Serializable]
    public class HtmlPage : BusinessObject
    {
        Guid m_guidHtmlPageId;
        string m_sPageUrl;  // primary key
        string m_sHtmlTitleTxt = string.Empty;
        string m_sMetaKeywordTxt = string.Empty;
        string m_sMetaDescriptionTxt = string.Empty;
        string m_sPageTitleTxt = string.Empty;
        DateTime m_dtCreatedUtcDttm;
        DateTime m_dtUpdatedUtcDttm;
        Guid m_guidParentHtmlPageId = Guid.Empty;
        bool m_bMetaNofollowInd;
        bool m_bMetaNoarchiveInd;
        bool m_bMetaNoindexInd;
        bool m_bUseNavigationPathInd;
        string m_sAliasPageUrl = string.Empty;
        string m_sPageSettings = string.Empty;
        string m_sRole = string.Empty;
        bool m_bIncludeHeaderImageInd = true;
        string m_sHeaderImageUrl = string.Empty;

        public static string GetRawUrl()
        {
            return HttpContext.Current.Request.RawUrl;
        }

        #region Public properties

        public Guid HtmlPageId
        {
            get { return m_guidHtmlPageId; }
        }
        public string PageUrl
        {
            get { return m_sPageUrl; }
            set { m_sPageUrl = value; }
        }
        public string RoutedPageUrl
        {
            get
            {
                //TODO - how to get the route data, so PageUrl can be inspected
                //var data = HttpContext.Current.Request.RequestContext.RouteData.RouteHandler;
                //string req = HttpContext.Current.
                return null;
            }
        }
        public string HtmlTitleTxt
        {
            get { return m_sHtmlTitleTxt; }
            set
            {
                if (!value.Equals(m_sHtmlTitleTxt))
                {
                    m_sHtmlTitleTxt = value;
                    SetDirty();
                }
            }
        }
        public string MetaKeywordTxt
        {
            get { return m_sMetaKeywordTxt; }
            set
            {
                if (!value.Equals(m_sMetaKeywordTxt))
                {
                    m_sMetaKeywordTxt = value;
                    SetDirty();
                }
            }
        }
        public string MetaDescriptionTxt
        {
            get { return m_sMetaDescriptionTxt; }
            set
            {
                if (!value.Equals(m_sMetaDescriptionTxt))
                {
                    m_sMetaDescriptionTxt = value;
                    SetDirty();
                }
            }
        }
        public string PageTitleTxt
        {
            get { return m_sPageTitleTxt; }
            set
            {
                if (!value.Equals(m_sPageTitleTxt))
                {
                    m_sPageTitleTxt = value;
                    SetDirty();
                }
            }
        }
        public DateTime CreatedUtcDttm
        {
            get { return m_dtCreatedUtcDttm; }
        }
        public DateTime UpdatedUtcDttm
        {
            get { return m_dtUpdatedUtcDttm; }
        }
        public Guid ParentHtmlPageId
        {
            get { return m_guidParentHtmlPageId; }
        }
        public bool MetaNofollowInd
        {
            get { return m_bMetaNofollowInd; }
            set
            {
                if (!value.Equals(m_bMetaNofollowInd))
                {
                    m_bMetaNofollowInd = value;
                    SetDirty();
                }
            }
        }
        public bool MetaNoarchiveInd
        {
            get { return m_bMetaNoarchiveInd; }
            set
            {
                if (!value.Equals(m_bMetaNoarchiveInd))
                {
                    m_bMetaNoarchiveInd = value;
                    SetDirty();
                }
            }
        }
        public bool MetaNoindexInd
        {
            get { return m_bMetaNoindexInd; }
            set
            {
                if (!value.Equals(m_bMetaNoindexInd))
                {
                    m_bMetaNoindexInd = value;
                    SetDirty();
                }
            }
        }
        public bool UseNavigationPathInd
        {
            get { return m_bUseNavigationPathInd; }
            set
            {
                if (!value.Equals(m_bUseNavigationPathInd))
                {
                    m_bUseNavigationPathInd = value;
                    SetDirty();
                }
            }
        }

        public string AliasPageUrl
        {
            get { return m_sAliasPageUrl; }
            set
            {
                if (!value.Equals(m_sAliasPageUrl))
                {
                    m_sAliasPageUrl = value;
                    SetDirty();
                }
            }
        }

        public string PageSettings
        {
            get { return m_sPageSettings; }
            set
            {
                if (!value.Equals(m_sPageSettings))
                {
                    m_sPageSettings = value;
                    SetDirty();
                }
            }
        }

        public string Role
        {
            get { return m_sRole; }
        }

        public bool IncludeHeaderImageInd 
        {
            get { return m_bIncludeHeaderImageInd; }
            set
            {
                if (!value.Equals(m_bIncludeHeaderImageInd))
                {
                    m_bIncludeHeaderImageInd = value;
                    SetDirty();
                }
            }
        }

        public string HeaderImageUrl 
        {
            get { return m_sHeaderImageUrl; }
            set
            {
                if (!value.Equals(m_sHeaderImageUrl))
                {
                    m_sHeaderImageUrl = value;
                    SetDirty();
                }
            }
        }

        #endregion Public properties

        public override void Load(System.Data.DataRow dr)
        {
            m_guidHtmlPageId = (Guid)dr["html_page_id"];
            m_sPageUrl = (string)dr["page_url"];
            if (DBNull.Value.Equals(dr["html_title_txt"]) == false)
                m_sHtmlTitleTxt = (string)dr["html_title_txt"];
            if (DBNull.Value.Equals(dr["meta_keyword_txt"]) == false)
                m_sMetaKeywordTxt = (string)dr["meta_keyword_txt"];
            if (DBNull.Value.Equals(dr["meta_description_txt"]) == false)
                m_sMetaDescriptionTxt = (string)dr["meta_description_txt"];
            if (DBNull.Value.Equals(dr["page_title_txt"]) == false)
                m_sPageTitleTxt = (string)dr["page_title_txt"];
            m_dtCreatedUtcDttm = (DateTime)dr["created_utc_dttm"];
            m_dtUpdatedUtcDttm = (DateTime)dr["updated_utc_dttm"];
            if (DBNull.Value.Equals(dr["parent_html_page_id"]) == false)
                m_guidParentHtmlPageId = (Guid)dr["parent_html_page_id"];
            m_bMetaNofollowInd = (bool)dr["meta_nofollow_ind"];
            m_bMetaNoarchiveInd = (bool)dr["meta_noarchive_ind"];
            m_bMetaNoindexInd = (bool)dr["meta_noindex_ind"];
            m_bUseNavigationPathInd = (bool)dr["use_navigation_path_ind"];
            if (DBNull.Value.Equals(dr["alias_page_url"]) == false)
                m_sAliasPageUrl = (string)dr["alias_page_url"];
            if (!dr.IsNull("page_settings"))
                m_sPageSettings = (string)dr["page_settings"];
            if (!dr.IsNull("role"))
                m_sRole = (string)dr["role"];
            m_bIncludeHeaderImageInd = (bool)dr["include_header_image_ind"];
            if (!dr.IsNull("header_image_url"))
                m_sHeaderImageUrl = (string)dr["header_image_url"];

            SetLoaded();
        }

        public override void Save()
        {
            if (Dirty)
            {
                StoredProcArgs args = new StoredProcArgs();
                args["pHtmlPageId"] = HtmlPageId;
                args["pPageUrl"] = PageUrl;
                if (string.IsNullOrEmpty(HtmlTitleTxt) == false)
                    args["pHtmlTitleTxt"] = HtmlTitleTxt;
                if (string.IsNullOrEmpty(MetaKeywordTxt) == false)
                    args["pMetaKeywordTxt"] = MetaKeywordTxt;
                if (string.IsNullOrEmpty(MetaDescriptionTxt) == false)
                    args["pMetaDescriptionTxt"] = MetaDescriptionTxt;
                if (string.IsNullOrEmpty(PageTitleTxt) == false)
                    args["pPageTitleTxt"] = PageTitleTxt;
                if (Guid.Empty.Equals(ParentHtmlPageId) == false)
                    args["pParentHtmlPageId"] = ParentHtmlPageId;
                args["pMetaNofollowInd"] = MetaNofollowInd;
                args["pMetaNoarchiveInd"] = MetaNoarchiveInd;
                args["pMetaNoindexInd"] = MetaNoindexInd;
                args["pUseNavigationPathInd"] = UseNavigationPathInd;
                if (string.IsNullOrEmpty(AliasPageUrl) == false)
                    args["pAliasPageUrl"] = AliasPageUrl;
                if (string.IsNullOrEmpty(PageSettings) == false)
                    args["pPageSettings"] = PageSettings;
                args["pIncludeHeaderImageInd"] = IncludeHeaderImageInd;
                if (string.IsNullOrEmpty(HeaderImageUrl) == false)
                    args["pHeaderImageUrl"] = HeaderImageUrl;
                GenericCud.ExecuteCud("proc_cm_SaveHtmlPage", args);

                SetDirty(false);
            }
        }

        public static HtmlPage GetPageDetail()
        {
            return GetPageDetail(GetRawUrl());
        }

        public static HtmlPage GetPageDetail(string sPageUrl)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pPageUrl"] = sPageUrl;
            DataSet oDs = GenericQry.GetDataSet("proc_cm_GetHtmlPageByUrl", args);
            HtmlPage oHtmlPage = new HtmlPage();
            if (oDs.Tables[0].Rows.Count > 0)
                oHtmlPage.Load(oDs.Tables[0].Rows[0]);
            return oHtmlPage;
        }

        public static HtmlPage GetPageDetailById(Guid guidHtmlPageId)
        {
            HtmlPage oHtmlPage = new HtmlPage();
            StoredProcArgs args = new StoredProcArgs();
            args["pHtmlPageId"] = guidHtmlPageId;
            using (DataSet oDs = GenericQry.GetDataSet("proc_cm_GetHtmlPageByUrlById", args))
            {
                if (oDs.Tables[0].Rows.Count > 0)
                    oHtmlPage.Load(oDs.Tables[0].Rows[0]);
            }
            return oHtmlPage;
        }
    }
}